
package io.github.hpsocket.soa.framework.leaf.segment.model;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import lombok.Getter;
import lombok.Setter;

/**
 * 双buffer
 */
public class SegmentBuffer
{
    @Getter
    @Setter
    private String key;
    @Getter
    private Segment[] segments; // 双buffer
    private volatile int currentPos; // 当前的使用的segment的index
    @Getter
    @Setter

    private volatile boolean nextReady; // 下一个segment是否处于可切换状态
    @Getter
    @Setter
    private volatile boolean initOk; // 是否初始化完成
    @Getter
    private final AtomicBoolean threadRunning; // 线程是否在运行中
    private final ReadWriteLock lock;

    @Getter
    @Setter
    private volatile int step;
    @Getter
    @Setter
    private volatile int minStep;
    @Getter
    @Setter
    private volatile long updateTimestamp;

    public SegmentBuffer()
    {
        segments = new Segment[] { new Segment(this), new Segment(this) };
        currentPos = 0;
        nextReady = false;
        initOk = false;
        threadRunning = new AtomicBoolean(false);
        lock = new ReentrantReadWriteLock();
    }

    public Segment getCurrent()
    {
        return segments[currentPos];
    }

    public int getCurrentPos()
    {
        return currentPos;
    }

    public int nextPos()
    {
        return (currentPos + 1) & 1;
    }

    public void switchPos()
    {
        currentPos = nextPos();
    }

    public Lock rLock()
    {
        return lock.readLock();
    }

    public Lock wLock()
    {
        return lock.writeLock();
    }

    @Override
    public String toString()
    {
        final StringBuilder sb = new StringBuilder("SegmentBuffer{");
        sb.append("key='").append(key).append('\'');
        sb.append(", segments=").append(Arrays.toString(segments));
        sb.append(", currentPos=").append(currentPos);
        sb.append(", nextReady=").append(nextReady);
        sb.append(", initOk=").append(initOk);
        sb.append(", threadRunning=").append(threadRunning);
        sb.append(", step=").append(step);
        sb.append(", minStep=").append(minStep);
        sb.append(", updateTimestamp=").append(updateTimestamp);
        sb.append('}');
        return sb.toString();
    }
}
